package app.server.project.main.service.service.rank;

import app.server.project.main.service.db.dao.user.UserRankDao;
import app.server.project.main.service.db.entity.cfg.RankCfgEntity;
import cn.hutool.core.date.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Date;

@Component
@Slf4j
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class WeekRank extends RankBase {
    @Autowired
    private UserRankDao userRankDao;

    public static void main(String[] args) {

        //获取本周最后一天
        var time = DateUtil.endOfWeek(new Date());
        //获取本周最后一天0点
        time = DateUtil.beginOfDay(time);
        //获取本周最后一天19点
        time = DateUtil.offsetHour(time, 19);
        if (time.isBefore(new Date())) {
            time = DateUtil.offsetDay(time, 7);
        }
        log.info("{}", time);
    }

    @Override
    public void flushNextTime(RankCfgEntity cfgEntity) {
        //获取本周最后一天
        var time = DateUtil.endOfWeek(new Date());
        //获取本周最后一天0点
        time = DateUtil.beginOfDay(time);
        //获取本周最后一天19点
        time = DateUtil.offsetHour(time, 19);
        if (time.isBefore(new Date())) {
            time = DateUtil.offsetDay(time, 7);
        }
        cfgEntity.setNextSettlementTime(time);
        cfgEntity.setCreateTime(LocalDateTime.now());
    }

    @Override
    public void doSettlement() {
        this.userRankDao.reset(this.getRankId());
    }
}
